﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using GoogleServicesLib;
using GoogleServicesLib.GeoCoding;
using System.Device.Location;
using System.Windows.Threading;

namespace GMapWithBingControl
{
    public partial class MainPage : PhoneApplicationPage
    {
        DispatcherTimer _timer;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this._timer = new DispatcherTimer();
            this._timer.Interval = new TimeSpan(0, 0, 15);
            this._timer.Tick+=new EventHandler(_timer_Tick);
        }

        private void TrafficJam_Loaded(object sender, RoutedEventArgs e)
        {
            this.sldZoom.Maximum = 21;
            this.sldZoom.Value = this.Gmap.ZoomLevel;
        }

        private void Gmap_MapZoom(object sender, Microsoft.Phone.Controls.Maps.MapZoomEventArgs e)
        {
            if (this.Gmap.ZoomLevel == this.sldZoom.Maximum)
            {
                this.Gmap.ZoomLevel = this.sldZoom.Maximum;
            }
            else
            {
                this.sldZoom.Value = this.Gmap.ZoomLevel;
            }
        }

        private void sldZoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.Gmap.ZoomLevel = this.sldZoom.Value;
        }

        private void btnZoomOut_Click(object sender, RoutedEventArgs e)
        {
            this.Gmap.ZoomLevel = this.Gmap.ZoomLevel--;
        }

        private void btnZoomIn_Click(object sender, RoutedEventArgs e)
        {
            this.Gmap.ZoomLevel = this.Gmap.ZoomLevel++;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            this.txtSearch.Visibility = Visibility.Visible;
            this.txtSearch.Focus();
        }

        private void txtSearch_LostFocus(object sender, RoutedEventArgs e)
        {
            this.txtSearch.Visibility = Visibility.Collapsed;
        }

        private async void txtSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                GeocodingRequest request = new GeocodingRequest();
                request.Address = this.txtSearch.Text;
                request.Sensor = "false";

                GeocodingResponse response = await GeocodingService.GetResponse(request);

                if (response.Status != ResponseStatus.Ok)
                    MessageBox.Show("Search not found");
                else
                {
                    double lat = Convert.ToDouble(response.Results[0].Geometry.Location.Latitude);
                    double lng = Convert.ToDouble(response.Results[0].Geometry.Location.Longitude);
                    this.lbMessage.Visibility = Visibility.Visible;
                    this.txtSearch.Visibility = Visibility.Collapsed;
                    this.lbMessage.Text = response.Results[0].FormattedAddress;
                    this.Gmap.SetView(new GeoCoordinate(lat, lng), 15);
                    this._timer.Start();
                }
            }
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            this.lbMessage.Visibility = Visibility.Collapsed;
            this._timer.Stop();
        }
    }
}